//-------
// 数据方法
//----
jQuery.extend({
    //阿拉伯数字转中文
    //$.typeConversion(n)
    typeConversion:function  (n) {
        var cnum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九',"十"];
        var s = '';
        n = '' + n; // 数字转为字符串
        for (var i = 0; i < n.length; i++) {
            s += cnum[parseInt(n.charAt(i))];
        }
        if (s.length == 2) {
            if (s.charAt(1) == cnum[0]) {
                s = s.charAt(0) + cnum[10];
                if (s == cnum[1] + cnum[10]) {
                    s = cnum[10]
                }
            } else if (s.charAt(0) == cnum[1]) {
                s = cnum[10] + s.charAt(1);
            }
        }
        return s;
    },
    //转千分号
    //$.commafy(n)
    commafy: function(num){
    	if(!$.isNumeric(num)){ return 0;}
        var decimalPart = '';
        num =(num+'').toString();
        if (num.indexOf('.') != -1) {
            decimalPart = '.' + num.split('.')[1];
            num = parseInt(num.split('.')[0]);
        }
        var array = num.toString().split('');
        var index = -3;
        while (array.length + index > 0 && array[array.length + index-1] !="-") {
            array.splice(index, 0, ',');
            index -= 4;
        }
        return array.join('') + decimalPart;
    },
    //去除千分号
    //$.delcommafy(n)
    delcommafy:function (num){
        if((num+"").trim()==""){ return"";}
        return (num +'').replace(/\,/ig,'');
    },
    //创建guid
    //$.createGuid()
    createGuid:function(){
        return 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0;
            var v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },
    //去除所有空格
    trimAll:function(txt){
        var result;
        result = txt.replace(/(^\s+)|(\s+$)/g,"");
        result = result.replace(/\s/g,"");
        return result;
    },
    //去除所有空格
    removeTrimEnter:function(txt){
        return $.trim(txt)
        .replace(/undefined/g,"")
        .replace(/[\r\n]/g, "")
        .replace(/\ +/g, " ");
    },
    //保留两位小数，不补0
    toDecimal: function (n) { 
        var f = parseFloat(n); 
        if (isNaN(f)) { return;  } 
        f = Math.round(n*100)/100; 
        return f; 
    },
    //转义html标签
    htmlEncode:function (text){
    	 var temp = document.createElement("div");
		(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
		var output = temp.innerHTML;
		temp = null;
		return output;
    },
    //反转义html标签
    htmlDecode:function (text){
    	 var temp = document.createElement("div"); 
		 temp.innerHTML = text; 
		 var output = temp.innerText || temp.textContent; 
		 temp = null; 
		 return output; 
    },
    //转义 script
    scriptEncode:function (obj){
    	 obj = JSON.stringify(obj);
		obj =obj.replace(/<script|<\/script/ig,function(c){ return "&lt;"+c.substring(1);});
		return JSON.parse(obj);
    },
    //导出
    exports:function(url,param){
    	var _url= __webConfig__.web_entrance+url +"?"+$.param(param||{},true);
    	window.open(_url,"下载");
    },
    //获取随机字符串
    getRandomString:function(){
        var len = len || 32;
        var $chars ="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
        var maxPos = $chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos));}
        return pwd;
    },
    //排序
    // arr = data
    // prop = 根据哪个字段进行排序
    // sort = desc / asc
    sortCompare:function(arr,prop,sort){
        return arr.sort(function (obj1, obj2) {
            var val1 = obj1[prop];
            var val2 = obj2[prop];
            if(sort=="desc"){
                if (val1 > val2) {
                    return -1;
                } else if (val1 < val2) {
                    return 1;
                } else {
                    return 0;
                }
            }else{
                if (val1 < val2) {
                    return -1;
                } else if (val1 > val2) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
    },
    //数组去重
 	uniques: function (arr) {
		var tmpArr=[], hash = {};
	  	for(var i = 0; i < arr.length; i++){
		   	if (!hash[arr[i]]){
				hash[arr[i]] = true; 
		    	tmpArr.push(arr[i]); 
		   	};
	  	};
	  	return tmpArr;
	}
});
